package cn.gzmli.dtt.common.utils;
import org.hibernate.SessionFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 获取spring容器，以访问容器中定义的其他bean
 * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a>  lyltiger
 * <a href="http://my.oschina.net/u/266547" class="referer" target="_blank">@since</a>  MOSTsView 3.0 2009-11-16
 */

@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    /**
     * 实现ApplicationContextAware接口的回调方法，设置上下文环境
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext){
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext(){
        return applicationContext;
    }

    /**
     * 获取对象
     * <a href="http://my.oschina.net/u/556800" class="referer" target="_blank">@return</a>  Object 一个以所给名字注册的bean的实例 (service注解方式，自动生成以首字母小写的类名为bean name)
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static <T> T getBean(String name, Class<T> cla) throws BeansException {
        return (T) applicationContext.getBean(name);
    }

    /**
     * 根据id获取对象
     * @param cla
     * @param id
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T>T getObjectWithSession(Class<T> cla, Serializable id) throws Exception {
        SessionFactory sessionFactory = (SessionFactory)getBean("sessionFactory");
        return (T)sessionFactory.getCurrentSession().get(cla, id);
    }

    /**
     * 根据id获取对象
     * @param cla
     * @param id
     * @param <T>
     * @return
     * @throws Exception
     */
    public static <T>T getObjectWithJDBC(Class<T> cla, Serializable id) throws Exception {
        JdbcTemplate jdbcTemplate = (JdbcTemplate)getBean("jdbcTemplate");
        StringBuilder tableName = new StringBuilder();
        tableName.append(cla.getName());
        StringBuilder sql = new StringBuilder();
        if(id instanceof String){
            sql.append("SELECT * FROM ").append(tableName.substring(tableName.lastIndexOf(".")+1)).append(" WHERE id='").append(id).append("'");
        }else{
            sql.append("SELECT * FROM ").append(tableName.substring(tableName.lastIndexOf(".")+1)).append(" WHERE id=").append(id);
        }
        try {
            return jdbcTemplate.queryForObject(sql.toString(), new BeanPropertyRowMapper<T>(cla));
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }
}